package gamefw.framework;

import gamefw.gameplugin.IGameAbstractFactory;
import gamefw.uiplugin.IUIAbstractFactory;

/**
 * The Class GameEngine.
 * This is the main class that is the entry point for the framework.
 * This class is responsible to register pairs of plugins, that is a Game Factory
 * and an User Interface Factory. For each of these pairs a new instance of
 * a GameManager is created. After initializing all the managers that are configured
 * in the system, it displays the main frame for the game, the user selects the game
 * that she/he wants to play and the correct GameManager is initialized and through it
 * the concrete GameLogic is accessed in order to start the game's main loop.
 */
public class GameEngine {

	/** The game engine instance. */
	private static GameEngine gameEngineInstance = null;

	/**
	 * Instantiates a new game engine.
	 */
	private GameEngine() {
	}
	
	/**
	 * This method calls the main loop of the concrete game logic that was selected by the user.
	 * 
	 * @param gameFactory the game factory
	 * @param uiFactory the UI factory
	 */
	public void startGame(IGameAbstractFactory gameFactory, IUIAbstractFactory uiFactory, String resourceType)
	throws Exception {
		GameManager manager = new GameManager(gameFactory, uiFactory, resourceType);
		manager.initGame();
	}
	
	/**
	 * Gets the single instance of GameEngine.
	 *
	 * @return single instance of GameEngine
	 */
	public static GameEngine getInstance() {
		if (GameEngine.gameEngineInstance == null) {
			GameEngine.gameEngineInstance = new GameEngine();
		}
		return GameEngine.gameEngineInstance;		
	}
}